package com.sduept.nwld.dataserver.controller.ledger;

import com.sduept.cim.ledger.manager.EquipmentLedgerManager;
import com.sduept.cim.ledger.manager.SubstationLedgerManager;
import com.sduept.cim.ledger.model.SubstationLedger;
import com.sduept.cim.model.core.Substation;
import com.sduept.cim.model.wires.Breaker;
import com.sduept.cim.model.wires.BusbarSection;
import com.sduept.cim.model.wires.Compensator;
import com.sduept.cim.model.wires.PowerTransformer;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.nwld.dataserver.manager.cim.CimManager;
import com.sduept.utils.PingYinUtil;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * 一次设备（母线、变压器、断路器、电抗器）台账信息查看
 * 变电站下的一次设备不进行配置，每次需要使用的时候，找到该设备所属变电站，获取该变电站的配置
 * @author Administrator
 */

@Named
@ViewScoped
public class EquipmentLedgerController extends AbstractController {

	private static final long serialVersionUID = 39286360984774012L;
	
	@Autowired
	private EquipmentLedgerManager ledgerM;
	@Autowired
	private CimManager cimM;
	@Autowired
	private SubstationLedgerManager stationLedgerM;
	
	private LazyDataModel<BusbarSection> busbarModel;
	private LazyDataModel<PowerTransformer> transModel;
	private LazyDataModel<Breaker> breakerModel;
	private LazyDataModel<Compensator> compensatorModel;
	
	private long rowCount = 0;
	private long rowCount2 = 0;
	private long rowCount3 = 0;
	private long rowCount4 = 0;
	
	private String subStation = null; // 所属变电站
	
	/**
	 * 变电站的ID，对象
	 */
	private Map<String,SubstationLedger> subMap = new HashMap<String, SubstationLedger>();
	
	@PostConstruct
	public void init() {
		initTable();
		subMap = stationLedgerM.getSubstationMap();
	}

	public void resetQueryStation() {
		subStation = null;
		initTable();
	}

	public void query() {
		initTable();
	}
	
	private void initTable() {
		initBusbarSectionTable();
		initPowerTransformerTable();
		initBreakerTable();
		initCompensatorTable();
	}
	
	private void initBusbarSectionTable() {
		busbarModel = new LazyDataModel<BusbarSection>() {
			private static final long serialVersionUID = 6065820294430715275L;
			@Override
			public List<BusbarSection> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
				return ledgerM.queryBusbarSectionByCondition(null,subStation, new int[] { first, first + pageSize });
			}
		};
		rowCount = ledgerM.countBusbarSection(null,subStation);
		busbarModel.setRowCount((int) rowCount);
	}
	
	private void initPowerTransformerTable() {
		transModel = new LazyDataModel<PowerTransformer>() {
			private static final long serialVersionUID = 6065820294430715275L;
			@Override
			public List<PowerTransformer> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
				return ledgerM.queryPowerTransformerByCondition(null,subStation, new int[] { first, first + pageSize });
			}
		};
		rowCount2 = ledgerM.countPowerTransformer(null,subStation);
		transModel.setRowCount((int) rowCount2);
	}
	private void initBreakerTable() {
		breakerModel = new LazyDataModel<Breaker>() {
			private static final long serialVersionUID = 6065820294430715275L;
			@Override
			public List<Breaker> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
				return ledgerM.queryBreakerByCondition(null,subStation, new int[] { first, first + pageSize });
			}
		};
		rowCount3 = ledgerM.countBreaker(null,subStation);
		breakerModel.setRowCount((int) rowCount3);
	}
	
	private void initCompensatorTable() {
		compensatorModel = new LazyDataModel<Compensator>() {
			private static final long serialVersionUID = 6065820294430715275L;
			@Override
			public List<Compensator> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
				return ledgerM.queryCompensatorByCondition(null,subStation, new int[] { first, first + pageSize });
			}
		};
		rowCount4 = ledgerM.countCompensator(null,subStation);
		compensatorModel.setRowCount((int) rowCount4);
	}
	
	public SubstationLedger getSubstationLedger(String subId) {
		SubstationLedger ledger = new SubstationLedger();
		if(StringUtils.isNotEmpty(subId)) {
			for (String key : subMap.keySet()) {
				if(key.equals(subId)) {
					ledger = subMap.get(key);
				}
			}
		}
		return ledger;
	}

	/**
	 * 变电站名称
	 * @param query
	 * @return
	 */
	public List<String> complateStation(String query) {
		List<Substation> allStations = cimM.findUsedAndUnUsedStations();
		List<String> names = new ArrayList<String>();
		names.add("全部");
		if (Pattern.matches("[a-z]{1,}", query)) {
			for (Substation station : allStations) {
				String nameHead = PingYinUtil.getPingYinHeader(station
						.getName());
				if (nameHead.toLowerCase().contains(query)) {
					names.add(station.getName());
				}
			}
		} else {
			for (Substation station : allStations) {
				if (station.getName().contains(query)) {
					names.add(station.getName());
				}
			}
		}
		return names;
	}
	
	public long getRowCount() {
		return rowCount;
	}

	public void setRowCount(long rowCount) {
		this.rowCount = rowCount;
	}

	public String getSubStation() {
		return subStation;
	}

	public void setSubStation(String subStation) {
		this.subStation = subStation;
	}

	public LazyDataModel<BusbarSection> getBusbarModel() {
		return busbarModel;
	}

	public void setBusbarModel(LazyDataModel<BusbarSection> busbarModel) {
		this.busbarModel = busbarModel;
	}

	public LazyDataModel<PowerTransformer> getTransModel() {
		return transModel;
	}

	public void setTransModel(LazyDataModel<PowerTransformer> transModel) {
		this.transModel = transModel;
	}

	public LazyDataModel<Breaker> getBreakerModel() {
		return breakerModel;
	}

	public void setBreakerModel(LazyDataModel<Breaker> breakerModel) {
		this.breakerModel = breakerModel;
	}

	public LazyDataModel<Compensator> getCompensatorModel() {
		return compensatorModel;
	}

	public void setCompensatorModel(LazyDataModel<Compensator> compensatorModel) {
		this.compensatorModel = compensatorModel;
	}

	public long getRowCount2() {
		return rowCount2;
	}

	public void setRowCount2(long rowCount2) {
		this.rowCount2 = rowCount2;
	}

	public long getRowCount3() {
		return rowCount3;
	}

	public void setRowCount3(long rowCount3) {
		this.rowCount3 = rowCount3;
	}

	public long getRowCount4() {
		return rowCount4;
	}

	public void setRowCount4(long rowCount4) {
		this.rowCount4 = rowCount4;
	}

}
